package com.ruoyi.web.controller.system;

import java.util.List;

import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.common.utils.ServletUtils;
import com.ruoyi.framework.web.service.TokenService;
import com.ruoyi.system.domain.RecordMoneys;
import com.ruoyi.system.service.IRecordMoneysService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.DistributionSub;
import com.ruoyi.system.service.IDistributionSubService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 分站提现Controller
 *
 * @author ruoyi
 * @date 2021-03-12
 */
@RestController
@RequestMapping("/system/sub")
public class DistributionSubController extends BaseController
{
    @Autowired
    private IDistributionSubService distributionSubService;
    @Autowired(required = false)
    private TokenService tokenService;
    @Autowired
    private IRecordMoneysService recordMoneysService;
    /**
     * 查询分站提现列表
     */
    //@PreAuthorize("@ss.hasPermi('system:sub:list')")
    @GetMapping("/list")
    public TableDataInfo list(DistributionSub distributionSub)
    {
        startPage();
        LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());
        Long userId = loginUser.getUser().getUserId();
        distributionSub.setUser_id(Integer.parseInt(userId.toString()));
        List<DistributionSub> list = distributionSubService.selectDistributionSubList(distributionSub);
        return getDataTable(list);
    }

    /**
     * 导出分站提现列表
     */
   // @PreAuthorize("@ss.hasPermi('system:sub:export')")
    @Log(title = "分站提现", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(DistributionSub distributionSub)
    {
        List<DistributionSub> list = distributionSubService.selectDistributionSubList(distributionSub);
        ExcelUtil<DistributionSub> util = new ExcelUtil<DistributionSub>(DistributionSub.class);
        return util.exportExcel(list, "sub");
    }

    /**
     * 获取分站提现详细信息
     */
   // @PreAuthorize("@ss.hasPermi('system:sub:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Integer id)
    {
        return AjaxResult.success(distributionSubService.selectDistributionSubById(id));
    }

    /**
     * 新增分站提现
     */
    //@PreAuthorize("@ss.hasPermi('system:sub:add')")
    @Log(title = "分站提现", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody DistributionSub distributionSub)
    {
        return toAjax(distributionSubService.insertDistributionSub(distributionSub));
    }

    /**
     * 修改分站提现
     */
    //@PreAuthorize("@ss.hasPermi('system:sub:edit')")
    @Log(title = "分站提现", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody DistributionSub distributionSub)
    {
        System.out.println("fenfenfenfenfenfenfenfenfenfen"+distributionSub.getDept_id());
        LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());
        Long userId = loginUser.getUser().getUserId();
        RecordMoneys rm = new RecordMoneys();
        rm.setTixiMoney(distributionSub.getKeMoney());
        rm.setUserId(Integer.parseInt(userId.toString()));
        rm.setTixiStarte("0");
        rm.setMoney_type("1");
        rm.setNickName("分站提现");
        rm.setShopsname(distributionSub.getDept_name());
        recordMoneysService.insertRecordMoneys(rm);
        distributionSub.setKeMoney(0.00);
        distributionSub.setSumMoneycopy(0.00);
        return toAjax(distributionSubService.updateDistributionSub(distributionSub));
    }

    /**
     * 删除分站提现
     */
    //@PreAuthorize("@ss.hasPermi('system:sub:remove')")
    @Log(title = "分站提现", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Integer[] ids)
    {
        return toAjax(distributionSubService.deleteDistributionSubByIds(ids));
    }
}
